package io.sdl.service.impl;

import io.sdl.common.service.impl.BaseServiceImpl;
import io.sdl.common.utils.CompleteLinkUtils;
import io.sdl.common.utils.ConvertUtils;
import io.sdl.common.utils.UploadFileUtils;
import io.sdl.dao.SdlPatientsArchivesDao;
import io.sdl.dto.SdlPatientsArchivesDTO;
import io.sdl.entity.SdlPatientsArchivesEntity;
import io.sdl.service.SdlPatientsArchivesService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@Service
public class SdlPatientsArchivesServiceImpl extends BaseServiceImpl<SdlPatientsArchivesDao, SdlPatientsArchivesEntity> implements SdlPatientsArchivesService {

	@Override
	public Integer getNum(Long patientId) {

		return baseDao.getNum(patientId);
	}

	@Override
	public List<SdlPatientsArchivesDTO> getPatientsBypatientId(Long patientId,String filterDate,  Integer page, Integer limit, String sortOrder) {

		List<SdlPatientsArchivesEntity> sdlPatientsArchivesList = baseDao.getPatientsBypatientId(patientId,filterDate, page - 1, limit, sortOrder);

		return ConvertUtils.sourceToTarget(sdlPatientsArchivesList, SdlPatientsArchivesDTO.class);
	}

	@Override
	public SdlPatientsArchivesEntity selectByArchivesId(Long id) {
		SdlPatientsArchivesEntity entity = baseDao.selectById(id);

		entity.setBendBackPhoto(entity.getBendBackPhoto()!=null? CompleteLinkUtils.CompleteLink(entity.getBendBackPhoto()):null);

		entity.setXPhoto(entity.getXPhoto()!=null?CompleteLinkUtils.CompleteLink(entity.getXPhoto()):null);

		entity.setRearPhoto(entity.getRearPhoto()!=null?CompleteLinkUtils.CompleteLink(entity.getRearPhoto()):null);

		entity.setBendForwardPhoto(entity.getBendForwardPhoto()!=null?CompleteLinkUtils.CompleteLink(entity.getBendForwardPhoto()):null);

		entity.setRightFrontPhoto(entity.getRightFrontPhoto()!=null?CompleteLinkUtils.CompleteLink(entity.getRightFrontPhoto()):null);

		entity.setLeftSidePhoto(entity.getLeftSidePhoto()!=null?CompleteLinkUtils.CompleteLink(entity.getLeftSidePhoto()):null);

		entity.setRightSidePhoto(entity.getRightSidePhoto()!=null?CompleteLinkUtils.CompleteLink(entity.getRightSidePhoto()):null);

		return entity;
	}

	@Override
	public Integer saveUploadedFile(MultipartFile file, Long id, String picName) {
		if (file.isEmpty()) {
			throw new IllegalArgumentException("上传文件不能为空");
		}

		try {


			String accessUrl = "/uploads/" + UploadFileUtils.uploadFile(file);
			System.out.println("复查记录id====================>"+id);
			// 使用selectForUpdate加锁
			SdlPatientsArchivesEntity entity = baseDao.selectById(id);
			switch (picName) {
				case SdlPatientsServiceImpl.PatientFileFields.REAR_PHOTO:
					entity.setRearPhoto(accessUrl);
					break;
				case SdlPatientsServiceImpl.PatientFileFields.BEND_BACK_PHOTO:
					entity.setBendBackPhoto(accessUrl);
					break;
				case SdlPatientsServiceImpl.PatientFileFields.BEND_FORWARD_PHOTO:
					entity.setBendForwardPhoto(accessUrl);
					break;
				case SdlPatientsServiceImpl.PatientFileFields.LEFT_SIDE_PHOTO:
					entity.setLeftSidePhoto(accessUrl);
					break;
				case SdlPatientsServiceImpl.PatientFileFields.RIGHT_SIDE_PHOTO:
					entity.setRightSidePhoto(accessUrl);
					break;
				case SdlPatientsServiceImpl.PatientFileFields.RIGHT_FRONT_PHOTO:
					entity.setRightFrontPhoto(accessUrl);
					break;
				case SdlPatientsServiceImpl.PatientFileFields.X_PHOTO:
					entity.setXPhoto(accessUrl);
					break;
				default:
					throw new IllegalArgumentException("不支持的文件字段: " + picName);
			}
			return baseDao.updateById(entity);
		} catch (IOException e) {
			throw new RuntimeException("文件存储失败", e);
		}
	}

	@Override
	public SdlPatientsArchivesEntity selectByPatientId(Long patientId) {

		return baseDao.selectByPatientId(patientId);
	}

	@Override
	public List<SdlPatientsArchivesEntity> getByPatientId(Long patientId) {
		return baseDao.getByPatientId(patientId);
	}

	@Override
	public Integer removeById(Long id) {
		return baseDao.removeById(id);
	}
}